import { Styles } from "styles.slint";

export component LineEdit inherits Rectangle {
    in property <string> placeholder-text;
    in property <length> font-size <=> text-input.font-size;
    in-out property <string> text <=> text-input.text;
    in-out property <brush> placeholder-color: Styles.palette.debug-bg;
    in property <bool> enabled <=> text-input.enabled;
    out property <bool> has-focus: text-input.has-focus;
    in property <InputType> input-type <=> text-input.input-type;
    in property <TextHorizontalAlignment> horizontal-alignment <=> text-input.horizontal-alignment;
    in property <bool> read-only <=> text-input.read-only;
    in property <int> font-weight <=> text-input.font-weight;
    in property <brush> text-color;
    in property <color> selection-background-color <=> text-input.selection-background-color;
    in property <color> selection-foreground-color <=> text-input.selection-foreground-color;
    in property <length> margin: Styles.sizes.p-lg;
    text-color: Styles.palette.window-fg;

    background: Styles.palette.layer-1;
    border-radius: Styles.sizes.r-md;
    border-width: 2px;
    border-color: text-input.has-focus ? Styles.palette.primary-bg : transparent;

    callback accepted(text: string);
    callback edited(text: string);
    callback key-pressed(event: KeyEvent) -> EventResult;
    callback key-released(event: KeyEvent) -> EventResult;

    public function set-selection-offsets(start: int, end: int) {
        text-input.set-selection-offsets(start, end);
    }

    public function select-all() {
        text-input.select-all();
    }

    public function clear-selection() {
        text-input.clear-selection();
    }

    public function cut() {
        text-input.cut();
    }

    public function copy() {
        text-input.copy();
    }

    public function paste() {
        text-input.paste();
    }

    // on width < 1px or if the `TextInput` is clipped it cannot be focused therefore min-width 1px
    min-width: 1px;
    min-height: Styles.sizes.h-md;
    clip: true;
    forward-focus: text-input;

    placeholder := Text {
        width: parent.width - root.margin * 2;
        x: root.margin;
        height: 100%;
        vertical-alignment: center;
        text: (root.text == "" && text-input.preedit-text == "") ? root.placeholder-text : "";
        font-size: text-input.font-size;
        font-italic: text-input.font-italic;
        font-weight: text-input.font-weight;
        font-family: text-input.font-family;
        color: root.placeholder-color;
        horizontal-alignment: root.horizontal-alignment;
        // `accessible-placeholder-text` is set on LineEdit already
        accessible-role: none;
    }

    text-input := TextInput {
        property <length> computed-x;

        x: root.margin + min(0, max(parent.width - self.width - self.text-cursor-width - 2 * root.margin, self.computed-x - root.margin));
        width: max(parent.width - self.text-cursor-width - root.margin * 2, self.preferred-width);
        vertical-alignment: center;
        single-line: true;
        color: root.text-color;
        text-cursor-width: 2px;

        selection-background-color: Styles.palette.primary-bg;

        cursor-position-changed(cursor-position) => {
            if cursor-position.x + self.computed_x < root.margin {
                self.computed_x = -cursor-position.x + root.margin;
            } else if cursor-position.x + self.computed_x > parent.width - root.margin - self.text-cursor-width {
                self.computed_x = parent.width - cursor-position.x - root.margin - self.text-cursor-width;
            }
        }

        accepted => {
            root.accepted(self.text);
        }

        edited => {
            root.edited(self.text);
        }

        key-pressed(event) => {
            root.key-pressed(event)
        }

        key-released(event) => {
            root.key-released(event)
        }
    }
}
